package com.heisenhub.file.utils;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.heisnenhub.common.module.utils.GsonUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 〈一句话功能简述〉<br> 
 * 〈Restful接口调用〉
 *
 * @author Chenwei
 * @create 2018/10/30
 * @since 1.0.0
 */

@Slf4j

public class HttpRestfulUtil {






    /**
     * @param url 调用接口的地址
     * @param headerMap 调用接口传入的方法体参数
     */
    public static String postDataByFormData(String url, Map<String, String> headerMap, HashMap<String,String> parmsMap ) {
        try {
            log.info("-----------------------------------post url：{},参数：{}  headerMap:{}", url, GsonUtils.toJson(parmsMap) , GsonUtils.toJson(headerMap));

            CloseableHttpClient httpClient = HttpClients.createDefault();
            HttpPost httpPost = new HttpPost(url);

            // 设置 form-data 参数
            // 设置 form-data 参数
            List<NameValuePair> formParams = new ArrayList<>();
            for (String key : parmsMap.keySet()) {
                formParams.add(new BasicNameValuePair(key, parmsMap.get(key)));
            }

            if(CollectionUtils.isNotEmpty(headerMap)){
                for (String key : headerMap.keySet()) {
                    httpPost.setHeader(key,headerMap.get(key));
                }
            }
            httpPost.setEntity(new UrlEncodedFormEntity(formParams, "UTF-8"));

            CloseableHttpResponse response = httpClient.execute(httpPost);
            try {
                String string = EntityUtils.toString(response.getEntity());

                log.info("-----------------------------------发起post 结束 url：{} ,retJson:{}" ,url,  string);
                return string ;

            } catch (IOException e) {
                log.info("【post请求 失败111】 {}",e);
                e.printStackTrace();
                return null;
            } finally {
                response.close();
            }
        } catch (IOException e) {
            log.info("【post请求 失败】 {}",e);
            e.printStackTrace();
            return null;
        }
    }


    public static String getAccessTokenApi(){
        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("grant_type", "client_credentials");

        HashMap<String, String> headerMap = new HashMap<>();
        headerMap.put("Authorization", "Basic YWlrZV9vcGVuYXBpX2NsaWVudDphaWtlQ2xpZW50QDEzMg11==");
        log.info("准备登录");
        // String url = HttpUtil.urlWithForm(hostUrl(sanhuaOauthProperty.getGetAccessTokenApiPath()), hashMap, Charset.defaultCharset(), false);
        //   String request = HttpUtils.request(Method.POST, url, headerBasic());
        String string = HttpRestfulUtil.postDataByFormData(" http://36.212.133.40:8008/car/oauth/token ", headerMap, hashMap);
        // JSONObject jsonObject = JSONObject.parseObject(request);

        return string;
    }

    public static void main(String[] args) {
        String accessTokenApi = getAccessTokenApi();
        System.out.println("11111111"+accessTokenApi);
    }


}
